   function init()
   {
   	if(productInfo.length > 0) {
		$('#productId').val(productInfo[0][0]);
		var colorId = productInfo[0][1];
		$('#colorId').val(colorId);
		var sizeId = productInfo[0][2];
		$('#sizeId').val(sizeId);

		setColorAvailability(sizeId);
		setSizeAvailability(colorId);
		setSelectedColor(colorId);
		setSelectedSize(sizeId);
   	}
   }

   $(document).ready(function() {
	$('#btn-addtocart').click(function() {
		var url = 'sherpa/api/cart/add/';
		var getvars = {
			'productId': $('#productId').val(),
			'quantity': $('#quantity option:selected').val()
		};
		// AJAX Request
		$.get(url, getvars, function(data) {
			refreshCart();
		});
		return false;
	});

	init();

	$('.product-colors a').mouseover(function() {
		var parts = $(this).attr('id').split('-');
		var colorId = parts[1];
		setSizeAvailability(colorId);
	});

	$('.product-colors a').mouseout(function() {
		setSizeAvailability($('#colorId').val());
	});

	$('.product-colors a').click(function() {
		var parts = $(this).attr('id').split('-');
		var colorId = parts[1];
		$('#colorId').val(colorId);
		setSelectedColor(colorId);
		swapProductImage();
		return false;
	});

	$('.product-sizes a').mouseover(function() {
		var parts = $(this).attr('id').split('-');
		var sizeId = parts[1];
		setColorAvailability(sizeId);
	});

	$('.product-sizes a').mouseout(function() {
		setColorAvailability($('#sizeId').val());
	});

	$('.product-sizes a').click(function() {
		var parts = $(this).attr('id').split('-');
		var sizeId = parts[1];
		$('#sizeId').val(sizeId);
		setSelectedSize(sizeId);
		return false;
	});

	$('.image-browser img').click(function() {
		var parts = $(this).attr('id').split('-');
		$('#imageId').val(parts[1]);
		$('.image-browser img').removeAttr('class');
		$(this).attr('class', 'selected');
		swapProductImage();
		return false;
	});

   });

   function _setProductInfo()
   {
   	if(productInfo.length > 0) {
   		var currentColorId = $('#colorId').val();
   		var currentSizeId = $('#sizeId').val();
		for(i=0; i<productInfo.length; i++) {
			colorId = productInfo[i][1];
			sizeId = productInfo[i][2];
			if((sizeId == currentSizeId) && (colorId == currentColorId)) {
				$('#btn-addtocart').removeAttr('disabled');
				$('#btn-addtocart').val('In den Warenkorb');
				$('#price').val(productInfo[i][3]);
				$('#productId').val(productInfo[i][0]);
				return;
			}
		}
   	}
   	$('#btn-addtocart').attr('disabled', 'disabled');
   	$('#btn-addtocart').val('Produkt nicht verfuegbar');
	$('#price').val(0);
	$('#productId').val(0);
   }

   function setSelectedSize(paramSizeId)
   {
   	var sizeId = 0;
   	var sizeElementId = '';
	for(i=0; i<availabilityInfo.sizes.length; i++) {
		sizeId = availabilityInfo.sizes[i];
		sizeElementId = '#sizeid-' + sizeId;
		if(sizeId == paramSizeId) {
			$(sizeElementId).attr('class', 'selected');
		} else {
			$(sizeElementId).removeAttr('class');
		}
	}
	_setProductInfo();
   }


   function setSelectedColor(paramColorId)
   {
   	var colorId = '';
	var colorElementId = '';
	for(i=0; i<availabilityInfo.colors.length; i++) {
		colorId = availabilityInfo.colors[i];
		colorElementId = '#colorid-' + colorId;
		if(colorId == paramColorId) {
			$(colorElementId).attr('class', 'colorid selected');
		} else {
			$(colorElementId).attr('class', 'colorid');
		}
	}
	_setProductInfo();
   }

   function setSizeAvailability(colorId)
   {
	if(availabilityInfo.colorToSize[colorId]) {
		var currentClass = '';
		for(i=0; i<availabilityInfo.sizes.length; i++) {
			var sizeId = availabilityInfo.sizes[i];
			var sizeElementId = '#sizeid-' + sizeId;
			currentClass = $(sizeElementId).attr('class');
			if(availabilityInfo.colorToSize[colorId][sizeId] == 0) {
				if(currentClass != '') {
					$(sizeElementId).attr('class', 'selected unavailable');
				} else {
					$(sizeElementId).removeAttr('class');
				}
			} else {
				if((currentClass == 'selected unavailable') || (currentClass == 'selected')) {
					$(sizeElementId).attr('class', 'selected');
				} else {
					$(sizeElementId).removeAttr('class');
				}
			}
		}
	}
   }

   function setColorAvailability(sizeId)
   {
	if(availabilityInfo.sizeToColor[sizeId]) {
		for(i=0; i<availabilityInfo.colors.length; i++) {
			var colorId = availabilityInfo.colors[i];
			var colorElementId = '#colorid-' + colorId;
			if(availabilityInfo.sizeToColor[sizeId][colorId] == 0) {
				$(colorElementId + " img").css('display', 'inline');
			} else {
				$(colorElementId + " img").css('display', 'none');
			}
		}
	}
   }

   function swapProductImage()
   {
	var url = 'sherpa/api/loadimage/';
	var vars = {
		'imageId': $('#imageId').val(),
		'colorApplicationId': $('#colorId').val()
	};
	$.getJSON(url, vars, function(json) {
		if(json.fileName) {
			$('#product-image').attr('src', productImagePath + json.fileName);
		}
	});
   }